package com.seawaterbt.ssm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.seawaterbt.ssm.entity.BasHeatcompany;
import com.seawaterbt.ssm.entity.GatherTemporary;
import com.seawaterbt.ssm.entity.ZComplainGather;
import com.seawaterbt.ssm.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class ZhoneComplainGather {
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
    @Autowired
    ZComplainGatherService zcomplainGatherService;
    @Autowired CzBasHeatcompanyService basHeatcompanyService;
    @Autowired IGatherTemporaryService gatherTemporaryService;

    public void pull(){
        QueryWrapper<ZComplainGather> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("GetTime",df.format(new Date()));
        List<ZComplainGather> zComplainGatherList = zcomplainGatherService.list();
        if (zComplainGatherList != null){
            for (ZComplainGather zComplainGather:zComplainGatherList){
                QueryWrapper<BasHeatcompany> heatcompanyQueryWrapper = new QueryWrapper<>();
                heatcompanyQueryWrapper.eq("code_province",zComplainGather.getCompanyCode());
                BasHeatcompany basHeatcompany = basHeatcompanyService.getOne(heatcompanyQueryWrapper);
                if (basHeatcompany != null){
                    QueryWrapper<GatherTemporary> gatherTemporaryQueryWrapper = new QueryWrapper<>();
                    gatherTemporaryQueryWrapper.likeRight("get_time",df.format(new Date())).eq("company_code",basHeatcompany.getCode());
                    GatherTemporary gatherTemporary = gatherTemporaryService.getOne(gatherTemporaryQueryWrapper);
                    if (gatherTemporary == null){
                        gatherTemporary = new GatherTemporary();
                    }
                    gatherTemporary.setComplainNow(zComplainGather.getAmountComplaint());
                    gatherTemporary.setComplainOver(zComplainGather.getAmountComplaintFinish());
                    gatherTemporary.setConsultNow(zComplainGather.getAmountConsultation());
                    gatherTemporary.setConsultOver(zComplainGather.getAmountConsultationFinish());
                    gatherTemporary.setServiceNow(zComplainGather.getAmountService());
                    gatherTemporary.setServiceOver(zComplainGather.getAmountServiceFinish());
                    gatherTemporary.setSource(4);//鼎控上传（原接口对接）
                    gatherTemporary.setGetTime(zComplainGather.getGetTime());
                    gatherTemporaryService.saveOrUpdate(gatherTemporary);
                }
            }
        }
    }


}
